package cn.edu.zzuli.wechat.user.controller;

import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.dto.StateAndLockDto;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.wechat.filter.NoAuth;
import cn.edu.zzuli.wechat.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户信息操作相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户个人中心的数值")
    @GetMapping("/getCount.do")
    public ResultInfo getCount(HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        Map<String,Object> map = userService.getInfo(uid);
        return Result.ok(map);
    }

    @ApiOperation("获取用户状态标志")
    @GetMapping("StateAndLock.do")
    public ResultInfo StateAndLock(HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        StateAndLockDto stateAndLock = userService.getStateAndLock(uid);
        return Result.ok(stateAndLock);
    }

    @NoAuth
    @ApiOperation("获取用户昵称")
    @GetMapping("getName.do")
    public ResultInfo getName(@RequestParam("toName") Integer uid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.select("nick_name","avatar_url").eq("uid",uid);
        User user = userService.getOne(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        System.out.println("hello");
        map.put("nickName",user.getNickName());
        map.put("avatarUrl",user.getAvatarUrl());
        return Result.ok(map);
    }

    @ApiOperation("充值")
    @GetMapping("recharge")
    public ResultInfo recharge(HttpSession session,Double bounty){
        Integer uid = (Integer) session.getAttribute("uid");
        userService.updateWealth(uid,bounty);
        return Result.ok();
    }


    @ApiOperation("获取充值后值")
    @GetMapping("getCharge")
    public ResultInfo getCharge(HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        Double wealth = userService.getWealth(uid);
        return Result.ok(wealth);
    }

    @NoAuth
    @GetMapping("test")
    public String test(){
        return "success";
    }
}
